/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.storage.android;

import android.content.Context;
import android.content.SharedPreferences;
import de.fuberlin.replication.storage.Storage;

/**
 * Android-specific abstract replication metadata storage implementation.
 */
public abstract class AndroidStorage implements Storage {
	private static final String PREFERENCES_NAME = "replication";

	private Context context;

	/**
	 * Constructor.
	 * 
	 * @param context the context of the application.
	 */
	public AndroidStorage(Context context) {
		this.context = context;
	}

	/**
	 * Returns the context of the application.
	 * 
	 * @return the context of the application.
	 */
	protected Context getContext() {
		return context;
	}

	/**
	 * Preferences-based object version counter implementation. Increases the object version counter by one and returns
	 * the new object version.
	 */
	@Override
	public long getVersion() {
		SharedPreferences preferences = getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);

		long version = preferences.getLong("version", 0) + 1;

		SharedPreferences.Editor editor = preferences.edit();
		editor.putLong("version", version);
		editor.commit();

		return version;
	}
}